import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
class Student{
    public String name;

    @Override
    public boolean equals(Object obj) {
        Student o = (Student)obj;
        return o.name.equals(this.name);
    }
}

//比较器
 class IntCmp implements Comparator<Integer>{
     @Override
     public int compare(Integer o1, Integer o2) {
         return o2.compareTo(o1);
     }
 }

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student();
        student1.name = "zhangsan";
        Student student2 = new Student();
        student2.name = "zhangsan";
        System.out.println(student1 == student2);//fasle 比较的地址
        System.out.println(student1.equals(student2));//fasle  需重写equals（） 重写后为true


    }

    public static void main12(String[] args) {
        int[] array = {10, 4, 2, 6, 15, 24, 9, 0, 4};
        int[] ret2 = maxLestK(array, 3);
        System.out.println(Arrays.toString(ret2));
    }
    //设计一个算法，找出数组中最小的k个数。以任意顺序返回这k个数均可。
    public int[] smallestK1(int[] array, int k) {
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(array.length);
        for(int i = 0; i < array.length; i++){
            priorityQueue.offer(array[i]);
        }
        int ret[] = new int[k];
        for(int i = 0;i < k; i++){
            ret [i] = priorityQueue.poll();
        }
        return ret;
    }

    //找出数组中最大的k个数
    public static int[] maxLestK(int [] array,int k){
        int[] ret = new int[k];
        if(array == null || k <= 0){
            return ret;
        }
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(new IntCmp());//大根堆
        for(int i = 0; i < k; i++){
            priorityQueue.offer(array[i]);
        }
        for(int i = k;i < array.length; i++){
            int top = priorityQueue.peek();
            if(array[i] > top){
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
        }
    }
        for(int i = 0; i < k; i++){
            ret [i] = priorityQueue.poll();
        }
        return ret;
    }

    public int[] smallestK2(int[] array, int k) {
        int[] ret = new int[k];
        if(array == null || k <= 0) {
            return ret;
        }
        PriorityQueue<Integer> priorityQueue =
                new PriorityQueue<>(new IntCmp());



        for (int i = 0; i < k; i++) {
            priorityQueue.offer(array[i]);
        }

        for (int i = k; i < array.length; i++) {
            int top = priorityQueue.peek();
            if(array[i] < top) {
                priorityQueue.poll();
                priorityQueue.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }







    public static void main2(String[] args) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
        priorityQueue.offer(10);
        priorityQueue.offer(2);
        priorityQueue.offer(7);
        priorityQueue.offer(6);
        priorityQueue.offer(16);
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        System.out.println(priorityQueue.poll());
        //输出默认为小根堆 2 6 7 10 16

    }
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initHeap(array);
        testHeap.createHeap();
        System.out.println("===");
        System.out.println("插入元素");
        //testHeap.offer(80);
        System.out.println("------");

        System.out.println("删除");
         int val = testHeap.poll();
        System.out.println(val);
        System.out.println("==");


    }
}
